package com.example.home.ui

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.ARouterPath
import com.example.common.CommonAdapter
import com.example.home.BR
import com.example.home.R
import com.example.home.databinding.FragmentTypeJumpBinding
import com.example.home.entity.VideoListEntity
import com.example.home.intent.VideoIntent
import com.example.home.state.VideoState
import com.example.home.viewmodel.VideoViewModel
import com.example.mvi_core.model.ui.BaseFragment
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@Route(path = ARouterPath.PAGE_JUMP)
class TypeJumpFragment : BaseFragment() {
    private val adapter by lazy{CommonAdapter<VideoListEntity>({R.layout.jp_item to BR.video})}
    @Autowired
    @JvmField
    var id:String="94349546935"
    private  val videoViewModel by lazy { VideoViewModel() }
    lateinit var binding:FragmentTypeJumpBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding=FragmentTypeJumpBinding.inflate(inflater)
        return binding.root
    }

    override fun initData() {
        super.initData()
         parentFragment?.arguments?.getBundle("")
        binding.jpRec.adapter=adapter
        ARouter.getInstance().inject(this)
        lifecycleScope.launchWhenCreated {
            videoViewModel.channel.send(VideoIntent.GetVideoList(id,1,10))
        }
        lifecycleScope.launch{
            videoViewModel.videoListObserve.collect {
                when(it){
                    is VideoState.ListSuccess->{
                        adapter.clear()
                      adapter+=it.list
                    }
                    is VideoState.Failure->{
                        Log.d("sss", "initData:${it.msg} ")
                    }
                    is VideoState.Loading->{
                        Log.d("sss", "initData: 加载中")
                    }
                    else ->{
                    }
                }
            }
        }


    }

}